// import clonedeep from 'lodash.clonedeep'
const SET_APP_MENUS = 'SET_APP_MENUS'
const SET_MENU_INFO = 'SET_MENU_INFO'
const SET_BREADCRUMB = 'SET_BREADCRUMB'

export const state = () => ({
  // 菜单
  appMenus: null,
  // 显示帮助信息
  showHelpInfo: false,
  // 菜单信息
  menuInfo: {
    // 当前路由
    currentPath: '',
    // 当前模块
    currentModule: {},
    // 二级菜单
    subMenus: [],
    // 默认选中子菜单
    defaultSelectedKeys: [],
    // 默认打开的分类
    defaultOpenKeys: []
  },
  // 面包屑
  breadcrumb: []
})
export const getters = {
  // 模块
  appModule: state => state.appMenus || [],
  // 当前模块
  currentModule: state => state.menuInfo.currentModule,
  // 面包屑
  breadcrumb: state => state.breadcrumb,
  // 二级菜单
  subMenus: state => state.menuInfo.subMenus,
  // 默认选中子菜单
  defaultSelectedKeys: state => state.menuInfo.defaultSelectedKeys,
  // 默认打开的分类
  defaultOpenKeys: state => state.menuInfo.defaultOpenKeys
}

export const actions = {
  async getAppMenus({ commit }, { $api, redirect }) {
    const res = await $api.common.getAppMenus()
    if (res.code === 1) {
      commit(SET_APP_MENUS, res.data)
    } else {
      redirect({
        path: '/login'
      })
    }
  }
}

export const mutations = {
  [SET_APP_MENUS](state, data) {
    state.appMenus = data
  },
  [SET_MENU_INFO](state, data) {
    state.menuInfo = Object.assign({}, state.menuInfo, data)
  },
  [SET_BREADCRUMB](state, data) {
    state.breadcrumb = data
  }
}
